<template>
  <div class="banner-container">
    <div class="header">
      <el-button
        size="large"
        @click="$router.push({ name: 'banneradd' })"
        :style="{ marginBottom: '20px' }"
        >添加轮播图 <el-icon><Plus /></el-icon
      ></el-button>
    </div>
    <el-table
      :header-cell-style="headerCellStyle"
      :cell-style="{ textAlign: 'center' }"
      :data="computedTableData"
      style="width: 100%"
      max-height="650"
    >
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column label="图片">
        <template #default="scope">
          <el-image :src="scope.row.img"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="链接" prop="link"></el-table-column>
      <el-table-column label="提示" prop="alt"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-popconfirm
            title="确定要删除吗?"
            confirm-button-text="确定"
            @confirm="confirmDel(scope.row.bannerid)"
          >
            <template #reference>
              <el-button size="small" type="danger">Delete</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--渲染分页-->
    <el-pagination
      background
      layout="prev,pager,next,total"
      :total="tableData.length"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
    />
  </div>
</template>

<script>
import { getBannerList, delBannerById } from '@/api/banner'
import { Plus } from '@element-plus/icons-vue'
export default {
  components: {
    Plus
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      headerCellStyle: {
        color: 'rgba(0,0,0,0.85)',
        fontWeight: '700',
        fontSize: '14px',
        textAlign: 'center',
        background: '#f8f8f8',
        height: '60px',
        borderRight: '1px solid #f0f0f0',
        transition: 'background .3s ease'
      },
      tableData: []
    }
  },
  computed: {
    computedTableData() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        (this.currentPage - 1) * this.pageSize + this.pageSize
      )
    }
  },
  methods: {
    confirmDel(bannerid) {
      delBannerById({ bannerid }).then(() => {
        this.getTableList()
      })
    },
    getTableList() {
      getBannerList().then((res) => {
        console.log(res)
        this.tableData = res.data
      })
    }
  },
  created() {
    this.getTableList()
  }
}
</script>

<style scoped lang="scss">
.el-pagination {
  justify-content: flex-end;
  margin-top: 32px;
}
.banner-container {
  height: 100%;
  padding: 24px;
  background: #fff;
}
</style>
